package com.ctyun.xstore.vod.template;

import java.io.IOException;
import java.util.EnumSet;

import com.ctyun.xstore.vod.AbstractVODClient;
import com.ctyun.xstore.vod.template.model.AudioParam;
import com.ctyun.xstore.vod.template.model.CreateAudioTranscodeTemplateRequest;
import com.ctyun.xstore.vod.template.model.CreateTranscodeTemplateResponse;
import com.ctyun.xstore.vod.template.model.CreateVideoTranscodeTemplateRequest;
import com.ctyun.xstore.vod.template.model.DeleteTranscodeTemplateRequest;
import com.ctyun.xstore.vod.template.model.DeleteTranscodeTemplateResponse;
import com.ctyun.xstore.vod.template.model.GetTranscodeTemplateRequest;
import com.ctyun.xstore.vod.template.model.GetTranscodeTemplateResponse;
import com.ctyun.xstore.vod.template.model.SearchTranscodeTemplateRequest;
import com.ctyun.xstore.vod.template.model.SearchTranscodeTemplateResponse;
import com.ctyun.xstore.vod.template.model.TemplateEnums.AudioCodecEnum;
import com.ctyun.xstore.vod.template.model.TemplateEnums.AudioSampleFormatEnum;
import com.ctyun.xstore.vod.template.model.TemplateEnums.VideoCodecEnum;
import com.ctyun.xstore.vod.template.model.TemplateEnums.VideoFormatEnum;
import com.ctyun.xstore.vod.template.model.TemplateEnums.VideoProfileEnum;

public final class TranscodeTemplateClient extends AbstractVODClient {

	public TranscodeTemplateClient(String accessKey, String secretKey) {
		super(accessKey, secretKey);
	}

	public TranscodeTemplateClient(String host, String accessKey, String secretKey) {
		super(host, accessKey, secretKey);
	}

	private void validateTemplateAudioParam(AudioParam audio) {
		if (AudioCodecEnum.MP3.equals(audio.getCodec())) {
			if ((null != audio.getChannels()) && (audio.getChannels() < 1 || audio.getChannels() > 2)) {
				throw new IllegalArgumentException("音频编码为MP3时，通道数只能为1或2！");
			}

			if ((null != audio.getSampleFormat())
					&& !EnumSet.of(AudioSampleFormatEnum.FLTP, AudioSampleFormatEnum.S32P, AudioSampleFormatEnum.S16P)
							.contains(audio.getSampleFormat())) {
				throw new IllegalArgumentException("音频编码为MP3时，音频采样格式仅支持fltp、s32p、s16p！");
			}
		}

		if (AudioCodecEnum.AAC.equals(audio.getCodec())) {
			if ((null != audio.getSampleFormat()) && !AudioSampleFormatEnum.FLTP.equals(audio.getSampleFormat())) {
				throw new IllegalArgumentException("音频编码为AAC时，音频采样格式仅支持fltp！");
			}
		}

		if (AudioCodecEnum.AMRWB.equals(audio.getCodec())) {
			if (null != audio.getChannels() && 1 != audio.getChannels().intValue()) {
				throw new IllegalArgumentException("音频编码为AMRWB时，通道数只能不设置或设置为1！");
			}
		}
	}

	/**
	 * 创建视频转码模板
	 *
	 * @param request 创建视频模板参数
	 * @return 创建模板的结果，注意：该结果仅返回 templateId，其他字段均为空
	 * @throws IOException 可能会发生 IO 异常。
	 */
	public CreateTranscodeTemplateResponse createVideoTranscodeTemplate(CreateVideoTranscodeTemplateRequest request)
			throws IOException {
		if (VideoFormatEnum.G3P.equals(request.getFormat())) {
			if (!VideoCodecEnum.H264.equals(request.getVideo().getCodec())) {
				throw new IllegalArgumentException("视频封装格式为3gp时视频编码只能为H264！");
			}
		}
		if (VideoProfileEnum.BASE_LINE.equals(request.getVideo().getProfile())) {
			if (!VideoCodecEnum.H264.equals(request.getVideo().getCodec())) {
				throw new IllegalArgumentException("仅当视频编码格式为H264时才能将视频参数profile设置为BASE_LINE！");
			}
		}
		if (Boolean.TRUE.equals(request.getEncrytion())) {
			if (!VideoFormatEnum.HLS.equals(request.getFormat())) {
				throw new IllegalArgumentException("只有当转码视频封装格式为HLS时才能对转码视频进行加密！");
			}
		}
		if (null != request.getAudio()) {
			this.validateTemplateAudioParam(request.getAudio());
		}
		return this.execute(request, CreateTranscodeTemplateResponse.class);
	}

	/**
	 * 创建音频转码模板
	 *
	 * @param request 创建音频转码模板参数
	 * @return 创建模板的结果，注意：该结果仅返回 templateId，其他字段均为空
	 * @throws IOException 可能会发生 IO 异常。
	 */
	public CreateTranscodeTemplateResponse createAudioTranscodeTemplate(CreateAudioTranscodeTemplateRequest request)
			throws IOException {
		if (null != request.getAudio().getCodec()) {
			if (!request.getFormat().getSupportedAudioCodec().contains(request.getAudio().getCodec())) {
				throw new IllegalArgumentException(String.format("音频模板封装格式为%s时不能设置编码格式为%s！", request.getFormat().name(),
						request.getAudio().getCodec().name()));
			}
		}
		this.validateTemplateAudioParam(request.getAudio());
		return this.execute(request, CreateTranscodeTemplateResponse.class);
	}

	/**
	 * 搜索转码模板
	 *
	 * @param request
	 * @return
	 * @throws IOException
	 */
	public SearchTranscodeTemplateResponse searchTranscodeTemplate(SearchTranscodeTemplateRequest request)
			throws IOException {
		return this.execute(request, SearchTranscodeTemplateResponse.class);
	}

	/**
	 * 获取单个转码模板
	 *
	 * @param templateId
	 * @return
	 * @throws IOException
	 */
	public GetTranscodeTemplateResponse getTranscodeTemplate(Long templateId) throws IOException {
		GetTranscodeTemplateRequest request = new GetTranscodeTemplateRequest(templateId);
		return this.execute(request, GetTranscodeTemplateResponse.class);
	}

	/**
	 * 删除转码模板
	 *
	 * @param templateId
	 * @return
	 * @throws IOException
	 */
	public DeleteTranscodeTemplateResponse deleteTranscodeTemplate(Long templateId) throws IOException {
		DeleteTranscodeTemplateRequest request = new DeleteTranscodeTemplateRequest(templateId);
		return this.execute(request, DeleteTranscodeTemplateResponse.class);
	}
}
